<template>
  <div>
    <common-head title="分类"></common-head>
    <div class="cates">
      <div class="left">
       <div
        v-for="(cate, index) in cates"
        :key="cate.id"
        @click="changeCate(index)"
        :class="['cate', {active: index===num}]">{{cate.name}}</div>
      </div>
      <div class="right">
        <div v-for="item in items" :key='item.id'>
          {{item.name}}
        </div>
      </div>
    </div>
    <my-tabbar></my-tabbar>
  </div>
</template>

<script>
import { getCates, getItemLists } from '@api'
import MyTabbar from '@components/MyTabbar'
import CommonHead from '@components/CommonHead'
export default {
  name: 'cates',
  data () {
    return {
      cates: [],
      num: 0,
      items: []
    }
  },
  created () {
    this.fetchAllCates()
  },
  methods: {
    changeCate (index) {
      // 切换分类
      this.num = index
      this.fetchItemLists()
    },
    fetchAllCates () {
      // 获取分类
      getCates().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItemLists()
        }
      })
    },
    fetchItemLists () {
      // 获取分类对于商品
      getItemLists({
        categoryId: this.cates[this.num].id
      }).then(res => {
        this.items = []
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    }
  },
  components: {
    MyTabbar,
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.cates{
  position: fixed;
  top:46px;
  bottom:50px;
  left:0;
  right:0;
 
  display:flex;
  .left{
    flex:1;
    border-right:1px solid #ccc;
    .cate{
      line-height: 40px;
      text-align: center;
      font-size:14px;
      color:#f1f1f1;
      background: rgb(194, 191, 146);
      border-bottom:1px solid #666;
      &.active{
        color:rgb(194, 191, 146);
        background: #f1f1f1;
        border-color:#fff;
      }
    }
  }
  .right{
    flex:4;
  
  }
}
</style>
